﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfBill.Controls
{
    /// <summary>
    /// UCPage.xaml 的交互逻辑
    /// </summary>
    public partial class UCPage : UserControl
    {
        /// <summary>
        /// 分页切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public delegate void PageEventHandler(Object sender, int page,int rp);
        public UCPage()
        {
            InitializeComponent();
            this.cmbPageSize.Items.Clear();
            for (int i = 0; i < pageSizeArray.Length; i++)
            {
                cmbPageSize.Items.Add(pageSizeArray[i]);
            }
            cmbPageSize.SelectedIndex = 1;
        }/// <summary>
         /// 分页改变事件
         /// </summary>
        public event PageEventHandler PageChanged;
        /// <summary>
        /// 每页数量改变事件
        /// </summary>
        public event PageEventHandler PageSizeChanged;
        private int count;
        private int page;
        private int pageSize = 20;
        private int pageCount = 1;
        /// <summary>
        /// 每页数量集合
        /// </summary>
        private int[] pageSizeArray = { 10, 20, 50, 100 };
        /// <summary>
        /// 总数
        /// </summary>
        public int Count
        {
            get
            {
                return count;
            }
            set
            {
                count = value;
                pageCount = count / pageSize + 1;
                this.lbPage.Content = "/" + pageCount + "";
            }
        }
        /// <summary>
        /// 当前页数
        /// </summary>
        public int Page
        {
            get { return page; }
            protected set
            {
                page = value;
                if (PageChanged != null)
                    PageChanged(this, Page,PageSize);
            }
        }
        /// <summary>
        /// 每页行数
        /// </summary>
        public int PageSize
        {
            get { return pageSize; }
            protected set
            {
                pageSize = value;
                pageCount = count / pageSize + 1;
                if (PageSizeChanged != null)
                    PageSizeChanged(this,Page, pageSize);
            }
        }

        private void txtPage_TextChanged(object sender, TextChangedEventArgs e)
        {
            int pg = 0;
            if (int.TryParse(txtPage.Text, out pg))
            {
                Page = pg;
            }

        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFristPage_Click(object sender, RoutedEventArgs e)
        {
            if (page != 1)
            {
                this.txtPage.Text = 1 + "";
            }
            //Page = 1;
        }
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrePage_Click(object sender, RoutedEventArgs e)
        {
            if (page > 1)
            {
                this.txtPage.Text = (page - 1) + "";
            }
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNextPage_Click(object sender, RoutedEventArgs e)
        {
            if (page < pageCount)
            {
                this.txtPage.Text = (page + 1) + "";
            }
        }
        /// <summary>
        /// 最后一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLastPage_Click(object sender, RoutedEventArgs e)
        {
            if (page < pageCount)
            {
                this.txtPage.Text = pageCount + "";
            }
        }

        /// <summary>
        /// 只允许输入数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPage_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9]+");// new Regex("[^0-9.-]+");
            e.Handled = re.IsMatch(e.Text);
        }

        private void cmbPageSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbPageSize != null && cmbPageSize.SelectedItem != null)
            {
                int size = 0;
                if (int.TryParse(cmbPageSize.SelectedValue.ToString(), out size))
                {
                    if (PageSize != size)
                        PageSize = size;
                }
            }
        }
    }
}
